#!/usr/bin/env python
#coding:utf8

import bank
from prettytable import PrettyTable
import man

shop_list = [
            ['Iphone6s',5800,],
            ['Mac_pro ',9000,],
            ['Bike    ',1000,],
            ['SSD_Disk',600,],
            ['Coffee  ',50,],
            ]

def shop_mall(name):
    shop_cat = {}
    print '''
        \033[35m 欢迎进入购物中心 \033[0m
          '''
    while True:
        print '\033[32m序号  名称     价格\033[0m'
        for k,v in enumerate(name):
            #print '%s  %s      %s' %(k,v,name[v])
            print '\033[32m %s : %s  %s \033[0m' % (k,v[0],v[1])

        user_input = raw_input('\033[33m请输入ID (Q\q退出) : \033[0m').strip()
        if user_input.isdigit() and int(user_input) < len(name):
            user_input = int(user_input)
            if name[user_input][0] not in shop_cat: #
                price = name[user_input][1] #提取价格
                amount = 1 #默认数量为 1
                shop_cat[name[user_input][0]] = {'price':price,'amount':amount,}
                #print shop_cat
            else:
                price = shop_cat[name[user_input][0]]['price'] + name[user_input][1] 
                shop_cat[name[user_input][0]]['amount'] += 1
                shop_cat[name[user_input][0]]['price'] += name[user_input][1]              
        elif user_input == 'q' or user_input == 'Q':
            if not len(shop_cat):
                man.man1()
            else:
                user_input = raw_input('\033[33m是否继续提交订单并支付？ y/n\033[0m').strip() #选择y是要结算
                if user_input == 'y':
                    #print 'linke band chine !!!'
                    for k,v in shop_cat.items(): #计算总价
                        if 'total' not  in shop_cat:
                            shop_cat['total'] = 0
                        #print '-->>',k,v
                        #print v['price']
                        shop_cat['total'] += v['price']
                    bank.shop_link_bank(shop_cat['total'])
                   # print shop_cat
                    #pass #调银行接口付款
                    break 
                elif user_input == 'n':
                    man.man1()
                else:
                    print '\033[31m您输入有误，请输入输入 y 或 n\033[0m'
                    continue
        else:
            print '\033[31m您输入有误，请重新输入\033[0m'
    #print shop_cat
    return shop_cat
        





if __name__ == '__main__':
    shop_mall(shop_list)
